package com.zyw.zhuangdian.controller;

import com.alibaba.fastjson.JSONObject;
import com.zyw.zhuangdian.constant.ServiceConstant;
import com.zyw.zhuangdian.enums.MsgCode;
import com.zyw.zhuangdian.event.SystemMessageSendEvent;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.in.MessageDtoIn;
import com.zyw.zhuangdian.pojo.in.SubscribeDtoIn;
import com.zyw.zhuangdian.pojo.in.UserDtoIn;
import com.zyw.zhuangdian.pojo.out.UserDtoOut;
import com.zyw.zhuangdian.service.MessageService;
import com.zyw.zhuangdian.service.SubscribeService;
import com.zyw.zhuangdian.service.UserService;
import com.zyw.zhuangdian.util.Assert;
import com.zyw.zhuangdian.util.Base64Util;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import com.zyw.zhuangdian.util.RespUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/subscribe")
@RequiredArgsConstructor
@Slf4j
public class SubscribeController {
    private final SubscribeService subscribeService;
    private final MessageService messageService;
    private final UserService userService;
    private final ApplicationContext applicationContext;

    /**
     * 新增关注
     * @param json
     * @return
     */
    @RequestMapping(value = "doSubscribe",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> addSubscribe(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"fromUid","关注者id不能为空");
        Assert.hasKeyAndValue(json,"toUid","被关注者id不能为空");

        SubscribeDtoIn in = BeanConvertUtil.covertBean(json, SubscribeDtoIn.class);

        // 防止自己关注自己
        if (in.getFromUid().equals(in.getToUid())){
            return RespUtil.OK(MsgCode.MSG_ILLEGAL_SUBSCRIBE);
        }

        // 获取用户名
        UserDtoIn userFrom = new UserDtoIn();
        userFrom.setUid(in.getFromUid());
        userFrom.setPrivilege(null);

        // 用户不存在
        Resp<List<UserDtoOut>> listFrom = userService.listUsers(userFrom);
        if (listFrom.hasNoData()){
            return RespUtil.OK(MsgCode.MSG_USER_NOT_EXIST);
        }

        String fromName = listFrom.getData().get(0).getNickname();

        // 创建消息 并发布事件
        MessageDtoIn msg = new MessageDtoIn();
        String content = fromName+" 关注了你";
        msg.setTitle(content);
        msg.setType(ServiceConstant.TYPE_SUBSCRIBE);
        msg.setContent(Base64Util.encode(content));
        msg.setFromUid(in.getFromUid());
        msg.setToUid(in.getToUid());
        applicationContext.publishEvent(new SystemMessageSendEvent(this,msg));

        Resp<Void> resp = subscribeService.addSubscribe(in);
        return RespUtil.OK(resp);
    }


    /**
     * 删除关注
     * @param json aid
     * @return
     */
    @RequestMapping(value = "cancelSubscribe",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> deleteSubscribe(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"fromUid","关注者id不能为空");
        Assert.hasKeyAndValue(json,"toUid","被关注者id不能为空");

        SubscribeDtoIn in = BeanConvertUtil.covertBean(json, SubscribeDtoIn.class);

        // 获取用户名
        UserDtoIn userFrom = new UserDtoIn();
        userFrom.setUid(in.getFromUid());
        userFrom.setPrivilege(null);

        // 用户不存在
        Resp<List<UserDtoOut>> listFrom = userService.listUsers(userFrom);
        if (listFrom.hasNoData()){
            return RespUtil.OK(MsgCode.MSG_USER_NOT_EXIST);
        }

        String fromName = listFrom.getData().get(0).getNickname();

        MessageDtoIn msg = new MessageDtoIn();
        String content = fromName+" 取消了关注";
        msg.setTitle(content);
        msg.setType(ServiceConstant.TYPE_SUBSCRIBE);
        msg.setContent(Base64Util.encode(content));
        msg.setFromUid(in.getFromUid());
        msg.setToUid(in.getToUid());
        applicationContext.publishEvent(new SystemMessageSendEvent(this,msg));

        Resp<Void> resp = subscribeService.deleteSubscribe(in);
        return RespUtil.OK(resp);
    }

    /**
     * 列出该用户关注列表
     * @param json
     * @return
     */
    @RequestMapping(value = "listSubscribes",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> listSubscribe(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"fromUid","关注者id不能为空");

        SubscribeDtoIn in = BeanConvertUtil.covertBean(json, SubscribeDtoIn.class);
        in.setToUid(null);
        Resp<List<UserDtoOut>> resp = subscribeService.listSubscribes(in);
        return RespUtil.OK(resp);
    }

    /**
     * 列出该用户关注数量
     * @param json
     * @return
     */
    @RequestMapping(value = "listSubscribesCount",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> listSubscribeCount(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"fromUid","关注者id不能为空");

        SubscribeDtoIn in = BeanConvertUtil.covertBean(json, SubscribeDtoIn.class);
        in.setToUid(null);
        Resp<Integer> resp = subscribeService.listSubscribesCount(in);
        return RespUtil.OK(resp);
    }
    /**
     * 列出关注者用户列表
     * @param json
     * @return
     */
    @RequestMapping(value = "listSubscribers",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> listSubscribers(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"toUid","被关注者id不能为空");

        SubscribeDtoIn in = BeanConvertUtil.covertBean(json, SubscribeDtoIn.class);
        in.setFromUid(null);
        Resp<List<UserDtoOut>> resp = subscribeService.listSubscribers(in);
        return RespUtil.OK(resp);
    }

    /**
     * 列出关注者用户数量
     * @param json
     * @return
     */
    @RequestMapping(value = "listSubscribersCount",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> listSubscribersCount(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"toUid","被关注者id不能为空");

        SubscribeDtoIn in = BeanConvertUtil.covertBean(json, SubscribeDtoIn.class);
        in.setFromUid(null);
        Resp<Integer> resp = subscribeService.listSubscribesCount(in);
        return RespUtil.OK(resp);
    }
}
